//
//  NetworkApi.swift
//  HinsLive
//
//  Created by haoxian on 2019/4/20.
//  Copyright © 2019 Hins. All rights reserved.
//

import Foundation
import RxSwift


private extension Network {
    static let headSalt = "HEADSALT666"
    static let footSalt = "FOOTSALT999"
}

private extension String {
    var saltMD5: String {
        let new = Network.headSalt + self + Network.footSalt
        return new.md5
    }
}

extension Network {
    static func fetchCategory() -> Observable<Event<FeedsCategory>> {
        return request(FeedsCategory.self, .category)
    }
    
    static func loadFeeds(_ req: FeedsEntityRequest) -> Observable<Event<FeedsEntity>> {
        return request(FeedsEntity.self, .feeds(req))
    }
    
    static func login(_ req: LoginRequest) -> Observable<Event<UserInfo>> {
        var newReq = req
        newReq.password = req.password.saltMD5
        return request(UserInfo.self, .login(newReq))
    }
    
    static func registerValidate(_ req: RegisterValidateRequest)-> Observable<Event<PBEmpty>> {
        return request(PBEmpty.self, .registerValidate(req))
    }
    
    static func register(_ req: RegisterRequest) -> Observable<Event<UserInfo>> {
        var newReq = req
        newReq.password = req.password.saltMD5
        return request(UserInfo.self, .register(newReq))
    }
    
    static func userAvatarChange(_ req: UserAvatarChangeRequest) -> Observable<Event<UserInfo>> {
        return request(UserInfo.self, .userAvatarChange(req))
    }
    
    static func userNameChange(_ req: UserNameChangeRequest) -> Observable<Event<UserInfo>> {
        return request(UserInfo.self, .userNameChange(req))
    }
    
    static func liveStart(_ req: LiveStartRequest) -> Observable<Event<FeedItem>> {
        return request(FeedItem.self, .liveStartRequest(req))
    }
}

